 import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/html_parser.dart';

class NewsContent extends StatefulWidget {
  
  Map arguments;
  NewsContent({Key key, this.arguments}) : super(key: key);

  @override
  _NewsContentState createState() => _NewsContentState(this.arguments);
}

class _NewsContentState extends State<NewsContent> {

  Map arguments;
  List _list = [];
  _NewsContentState(this.arguments);

  @override
  void initState() {
    super.initState();
    print(this.arguments);
    this._getData();
  }

  _getData() async{
    var response = await Dio().get('http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid=${this.arguments['aid']}');
    
    setState(() {
      this._list = json.decode(response.data)['result'];
    });
    print(this._list[0].keys.toList());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('新闻详情') 
      ), 
      body: this._list.length > 0 ?
        ListView(
          children: <Widget>[
            // Text(this._list[0]['title']),
            // Text(this._list[0]['content']),
            Html(
              data: """
                ${this._list[0]['content']}
              """,
              //Optional parameters:
              padding: EdgeInsets.all(8),
              backgroundColor: Colors.white70,
              defaultTextStyle: TextStyle(fontFamily: 'serif'),
              linkStyle: const TextStyle(
                color: Colors.redAccent 
              ),
              onLinkTap: (url) {
                // open url in a webview
              }
            )
          ], 
        )
      : _getMoreWidget()
    );
  }

  //加载中的圈圈
  Widget _getMoreWidget(){
    return Center(
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            CircularProgressIndicator(
              strokeWidth: 1, 
            ),
            Text(
              '加载中...',
              style: TextStyle(fontSize:16)
            )
          ], 
        ),
      ),
    ); 
  }
}